package hjw.com.controller.stu;

import hjw.com.dao.MarkMapper;
import hjw.com.dao.TaskMapper;
import hjw.com.dao.UserMapper;
import hjw.com.entity.TaskInfo;
import hjw.com.entity.User;
import hjw.com.entity.mark.MarkInfo;
import hjw.com.entity.stu.MyCourse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/stu")
@SuppressWarnings("all")
public class StuController {

    @Autowired
    UserMapper userMapper;
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    MarkMapper markMapper;

    @GetMapping("/markInfo")
    public String doMark(HttpSession session,Model model){
        User user = (User)session.getAttribute("user");
        List<MarkInfo> markInfos = markMapper.findOne(user.getPhone());
        if (!markInfos.isEmpty()){
            model.addAttribute("markInfos",markInfos);
        }
        return "stu/show";
    }

    @GetMapping("/myCourse")
    public String goFindCouse(HttpSession session,Model model){
        //根据当前用户的clzno查询该用户班级的课程
        List<TaskInfo> taskInfos = taskMapper.findByClzno(((User) session.getAttribute("user")).getClz().getClzno());

        System.out.println(taskInfos);

        if (!taskInfos.isEmpty()){
            model.addAttribute("taskInfos",taskInfos);
        }
        return "stu/myCourse";
    }

    @GetMapping("/go2Update")
    public String go2Update(){
        return "stu/update";
    }

    @PostMapping("/doUpdate")
    public String doUpdate(String name, String pwd, HttpServletRequest req, Model model){
        //无数据传参拦截修改
        if (name == null && pwd == null){
            model.addAttribute("state","提示：未作修改!!!");
            return "main/index";
        }

        //此处用户不允许修改手机号
        User user = (User) req.getSession().getAttribute("user");

        //判断数据是否修改
        if (!pwd.equals(user.getPwd()) || !name.equals(user.getUname())){

            if (!name.equals(user.getUname())){
                // 有且仅有名字修改
                // 更新session信息
                user.setUname(name);
            }
            if (!pwd.equals(user.getPwd())){
                // 有且仅有密码修改
                // 更新session信息
                user.setPwd(pwd);
            }
            int key = userMapper.updateById(user);
            System.out.println("user.getPhone()" + userMapper.findByPhone(user.getPhone()));
            model.addAttribute("state",key > 0 ? "数据提示：修改成功!!!":"数据提示：修改失败!!!");
            return "main/index";
        }
        model.addAttribute("state","提示：未作修改!!!");
        return "main/index";
    }

}
